﻿@page "/formitems"
@attribute [Authorize(ABPLHQCSPermissions.FormItems.Default)]
@using ABPLHQCS.Permissions
@using Microsoft.AspNetCore.Antiforgery
@using Volo.Abp.Application.Dtos
@using ABPLHQCS.Application
@using ABPLHQCS.Localization
@using Microsoft.Extensions.Localization
@using Volo.Abp.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web
@using ABPLHQCS.FormItems
@* @inject ABPLHQCS.Categories.FormCategoryAppService categoryApp *@
@inject NavigationManager NavigationManager
@inject IStringLocalizer<ABPLHQCSResource> L
@inject AbpBlazorMessageLocalizerHelper<ABPLHQCSResource> LH
@inherits AbpCrudPageBase<IFormItemAppService, FormItemDto, Guid, PagedAndSortedResultRequestDto, CreateUpdateFormItemDto>
@inject ABPLHQCS.Categories.FormCategoryAppService categoryAppService
@inject IAntiforgery antiforgery

<Card>
    <CardHeader>
        <Row Class="justify-content-between">
            <Column ColumnSize="ColumnSize.IsAuto">
                <h2>@L["FormItemsManage"]</h2>
            </Column>
            <Column ColumnSize="ColumnSize.IsAuto">
                @if (HasCreatePermission)
                {
                    <Button Color="Color.Primary" Clicked="OpenCreateModalAsync">@L["NewFormItem"]</Button>
                }
                
              
            </Column>
        </Row>
    </CardHeader>
    <CardBody>
        <DataGrid TItem="FormItemDto" Data="Entities" ReadData="OnDataGridReadAsync" CurrentPage="CurrentPage" TotalItems="TotalCount"
                  ShowPager="true" PageSize="PageSize" 
        
        >
            <DataGridColumns>
                <DataGridEntityActionsColumn TItem="FormItemDto" @ref="@EntityActionsColumn">
                    <DisplayTemplate>
                        <EntityActions TItem="FormItemDto" EntityActionsColumn="@EntityActionsColumn">
                            <EntityAction TItem="FormItemDto" Text="@L["Edit"]" Visible=HasUpdatePermission
                                          Clicked="()=>OpenEditModalAsync(context)" />
                            <EntityAction TItem="FormItemDto" Text="@L["Delete"]" Visible=HasDeletePermission
                                          Clicked="()=>DeleteEntityAsync(context)"
                                          ConfirmationMessage="()=>GetDeleteConfirmationMessage(context)" />
                        </EntityActions>
                    </DisplayTemplate>
                </DataGridEntityActionsColumn>
                <DataGridColumn TItem="FormItemDto"
                                Field="@nameof(FormItemDto.FormName)"
                                Caption="表单名称">

                </DataGridColumn>

               <DataGridColumn TItem="FormItemDto"
                        Caption="填写">
                       <DisplayTemplate>
                        <a  target="_blank" href="@($"fdesign/fbpreview.html?formId={ (context as FormItemDto).Id }")">填写表单</a>
                       </DisplayTemplate>
         @*    <DataGridColumnTemplate>
                        <a href="@($"fdesign/fbpreview.html?formId=/{  }")">填写表单</a>
            </DataGridColumnTemplate> *@
        </DataGridColumn>
            </DataGridColumns>
        </DataGrid>
    </CardBody>
</Card>
<Modal @ref="@CreateModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <Form>
            <ModalHeader>
                <ModalTitle>@L["NewFormItem"]</ModalTitle>
                <CloseButton Clicked="CloseCreateModalAsync" />
            </ModalHeader>
            <ModalBody>
                <Validations @ref="@CreateValidationsRef" Model="@NewEntity" ValidateOnLoad="false">
                    <Field>
                        <FieldLabel>@L["FormName"]</FieldLabel>
                        <TextEdit @bind-Text="NewEntity.FormName"></TextEdit>
                    </Field>
                    <Field>
                        <FieldLabel>@L["FormContent"]</FieldLabel>
                        <MemoEdit @bind-Text="NewEntity.FormContent"></MemoEdit>
                    </Field>
                    <Field>
                        <FieldLabel>@L["FormCategoryName"]</FieldLabel>
                        @if (FormCategories != null){
                        <Select   TValue="Guid"   Multiple="false"  @bind-SelectedValue="NewEntity.FormCategoryId"  >
                            @foreach (var category in FormCategories)
                            {
                                <SelectItem Value="@category.Id">@category.CategoryName</SelectItem>
                            }
                        </Select>
                        }
                    </Field>
                    <Link To="./fdesign/index.html" Target="Target.Blank">设计器</Link>
                </Validations>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Secondary"
                        Clicked="CloseCreateModalAsync">
                    @L["Cancel"]
                </Button>
                <Button Color="Color.Primary"
                        Type="@ButtonType.Submit"
                        PreventDefaultOnSubmit="true"
                        Clicked="CreateEntityAsync">
                    @L["Save"]
                </Button>
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>
<Modal @ref="@EditModal">
    <ModalBackdrop />
    <ModalContent IsCentered="true">
        <Form>
            <ModalHeader>
                <ModalTitle>@EditingEntity.FormName</ModalTitle>
                <CloseButton Clicked="CloseEditModalAsync" />
            </ModalHeader>
            <ModalBody>
                <Validations @ref="@EditValidationsRef" Model="@EditingEntity" ValidateOnLoad="false">
                    <Validation MessageLocalizer="@LH.Localize">
                        <Field>
                            <FieldLabel>@L["FormName"]</FieldLabel>
                            <TextEdit @bind-Text="@EditingEntity.FormName">
                                <Feedback>
                                    <ValidationError />
                                </Feedback>
                            </TextEdit>
                        </Field>
                    </Validation>
                    <Field>
                        <FieldLabel>@L["FormContent"]</FieldLabel>
                        <MemoEdit @bind-Text="EditingEntity.FormContent"></MemoEdit>
                        <Field>
                            <FieldLabel>@L["FormCategoryName"]</FieldLabel>
                            @if (FormCategories != null)
                            {
                                <Select TValue="Guid" @bind-SelectedValue="EditingEntity.FormCategoryId" >
                                    @foreach (var category in FormCategories)
                                    {
                                        <SelectItem Value="@category.Id">@category.CategoryName</SelectItem>
                                    }
                                </Select>
                            }
                        </Field>
                    </Field>
                    <Link To="./fdesign/index.html" Target="Target.Blank">设计器</Link>
                </Validations>
            </ModalBody>
            <ModalFooter>
                <Button Color="Color.Secondary"
                        Clicked="CloseEditModalAsync">
                    @L["Cancel"]
                </Button>
                <Button Color="Color.Primary"
                        Type="@ButtonType.Submit"
                        PreventDefaultOnSubmit="true"
                        Clicked="UpdateEntityAsync">
                    @L["Save"]
                </Button>
            </ModalFooter>
        </Form>
    </ModalContent>
</Modal>
@code {
    protected IReadOnlyList<ABPLHQCS.Categories.FormCategoryDto>? FormCategories;

    protected Guid selectedValue;

    public   FormItem()
    {
        CreatePolicyName = ABPLHQCSPermissions.FormItems.Create;
        UpdatePolicyName = ABPLHQCSPermissions.FormItems.Edit;
        DeletePolicyName = ABPLHQCSPermissions.FormItems.Delete;
        LocalizationResource = typeof(ABPLHQCSResource);
        NewEntity.FormCategoryId = Guid.Parse("3a14952b-d790-fd97-860a-9b242246af02");


    }

    protected  async Task SetPermissionsAsync_again()
    {
        if (CreatePolicyName != null)
        {
            HasCreatePermission = await AuthorizationService.IsGrantedAsync(CreatePolicyName);
        }

        if (UpdatePolicyName != null)
        {
            HasUpdatePermission = await AuthorizationService.IsGrantedAsync(UpdatePolicyName);
        }

        if (DeletePolicyName != null)
        {
            HasDeletePermission = await AuthorizationService.IsGrantedAsync(DeletePolicyName);
        }
    }


    protected Task OnSelectedValueChanged( Guid value)
    {
        NewEntity.FormCategoryId = value;

        return Task.CompletedTask;

    }


    protected void OnRowDbClicked(DataGridRowMouseEventArgs<FormItemDto> eventArgs)
    {
        string url = $"fdesign/fbpreview.html?formId={eventArgs.Item.Id}";

        NavigationManager.NavigateTo(url, true);

    }

    protected override async Task OnInitializedAsync()
    {
        await GetFormCategoriesAsync();
        await SetPermissionsAsync_again();
    }


    private async Task GetFormCategoriesAsync()
    {
        
        var result = await categoryAppService.GetListAsync(new PagedAndSortedResultRequestDto { SkipCount = 0, Sorting = "", MaxResultCount = 100 });
        FormCategories = result.Items;
    }

}
